library(stargazer)

##---Loading in data ----
setwd("~/Replication Code/")

anes9297 <- read.dta("./data/anes9297_subset.dta")

ccap08 <- read.dta("./data/ccap08_subset.dta")

ccap12 <- read.dta("./data/ccap12_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")


#-ANES9294----
anes9294.ps <- na.omit(anes9297[c("white_int92", "white_int94",
                                   "pid92_7scaled", "pid94_7scaled", 
                                   "rr92_scaled", "rr94_scaled", "V940005",
                                   "ps_sc_92")])

# Code high PK folks including median with high
anes9294.ps$pk_hi_atabvMed <- ifelse(anes9294.ps$ps_sc_92 >= median(anes9294.ps$ps_sc_92),
                                     1, 0)

### Low PS
# PID on RR
m1.ps.anes.lo <- lm(rr94_scaled ~  pid92_7scaled  + rr92_scaled, 
                    data = anes9294.ps, weights = V940005, 
                    subset = (white_int92 == white_int94) & pk_hi_atabvMed == 0)
summary(m1.ps.anes.lo)

# RR on PID
m10.ps.anes.lo <- lm(pid94_7scaled ~  pid92_7scaled  + rr92_scaled, 
                     data = anes9294.ps, weights = V940005, 
                     subset = (white_int92 == white_int94) & pk_hi_atabvMed == 0)
summary(m10.ps.anes.lo)


### high PS
# PID on RR
m1.ps.anes.hi <- lm(rr94_scaled ~  pid92_7scaled  + rr92_scaled, 
                    data = anes9294.ps, weights = V940005, 
                    subset = (white_int92 == white_int94) & pk_hi_atabvMed == 1)
summary(m1.ps.anes.hi)


# RR on PID
m10.ps.anes.hi <- lm(pid94_7scaled ~  pid92_7scaled  + rr92_scaled, 
                     data = anes9294.ps, weights = V940005, 
                     subset = (white_int92 == white_int94) & pk_hi_atabvMed == 1)
summary(m10.ps.anes.hi)

#### Comparing hi-lo
# PID on RR
m1.ps.anes.lohi <- lm(rr94_scaled ~  pid92_7scaled*pk_hi_atabvMed  + rr92_scaled*pk_hi_atabvMed, 
                      data = anes9294.ps, weights = V940005, 
                      subset = (white_int92 == white_int94))
summary(m1.ps.anes.lohi)

# RR on PID
m10.ps.anes.lohi <- lm(pid94_7scaled ~  pid92_7scaled*pk_hi_atabvMed  + rr92_scaled*pk_hi_atabvMed, 
                       data = anes9294.ps, weights = V940005, 
                       subset = (white_int92 == white_int94))
summary(m10.ps.anes.lohi)


#--VSG----
vsg.ps <- na.omit(vsg.dat[c("pk_12", "rr_sc_12", "rr_sc_16", "pid7_sc_12", "pid7_sc_16", "weight")])

# Code high PK folks including median with high
vsg.ps$pk_hi_atabvMed <- ifelse(vsg.ps$pk_12 >= median(vsg.ps$pk_12),
                                1, 0)


## Low
# PID on RR
m1.ps.vsg.lo <- lm(rr_sc_16 ~ pid7_sc_12 + rr_sc_12, 
                   data = vsg.ps, weights = weight,
                   subset = pk_hi_atabvMed == 0)
summary(m1.ps.vsg.lo)

# RR on PID
m10.ps.vsg.lo <- lm(pid7_sc_16 ~ pid7_sc_12 + rr_sc_12, 
                    data = vsg.ps, weights = weight,
                    subset = pk_hi_atabvMed == 0)
summary(m10.ps.vsg.lo)

## high
# PID on RR
m1.ps.vsg.hi <- lm(rr_sc_16 ~ pid7_sc_12 + rr_sc_12, 
                   data = vsg.ps, weights = weight,
                   subset = pk_hi_atabvMed == 1)
summary(m1.ps.vsg.hi)

# RR on PID
m10.ps.vsg.hi <- lm(pid7_sc_16 ~ pid7_sc_12 + rr_sc_12, 
                    data = vsg.ps, weights = weight,
                    subset = pk_hi_atabvMed == 1)
summary(m10.ps.vsg.hi)

#### Comparing hi-lo
# PID on RR
m1.ps.vsg.lohi <- lm(rr_sc_16 ~ pid7_sc_12*pk_hi_atabvMed + rr_sc_12*pk_hi_atabvMed, 
                     data = vsg.ps, weights = weight)
summary(m1.ps.vsg.lohi)

# RR on PID
m10.ps.vsg.lohi <- lm(pid7_sc_16 ~ pid7_sc_12*pk_hi_atabvMed + rr_sc_12*pk_hi_atabvMed, 
                      data = vsg.ps, weights = weight)
summary(m10.ps.vsg.lohi)

#----------------------------------------------------------------------------------------------------#
#-Table 4----
# Formatting for tables
m1.ps.anes.lo_p <- m1.ps.anes.lo
m10.ps.anes.lo_p <- m10.ps.anes.lo
m1.ps.anes.hi_p <- m1.ps.anes.hi
m10.ps.anes.hi_p <- m10.ps.anes.hi
m1.ps.vsg.lo_p <- m1.ps.vsg.lo
m10.ps.vsg.lo_p <- m10.ps.vsg.lo
m1.ps.vsg.hi_p <- m1.ps.vsg.hi
m10.ps.vsg.hi_p <- m10.ps.vsg.hi

names(m1.ps.anes.lo_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m10.ps.anes.lo_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m1.ps.anes.hi_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m10.ps.anes.hi_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m1.ps.vsg.lo_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m10.ps.vsg.lo_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m1.ps.vsg.hi_p$coefficients) <- names(coef(m1.ps.anes.lo_p))
names(m10.ps.vsg.hi_p$coefficients) <- names(coef(m1.ps.anes.lo_p))

stargazer(m1.ps.anes.lo_p, m10.ps.anes.lo_p, m1.ps.anes.hi_p, m10.ps.anes.hi_p, m1.ps.vsg.lo_p, m10.ps.vsg.lo_p, m1.ps.vsg.hi_p, m10.ps.vsg.hi_p,
          title = "Political Awareness's Moderating Effect on the relationship between Whites' Partisanship and Racial Resentment",
          covariate.labels = c("Partisanship$_{t-1}$",
                               "Racial Resentment$_{t-1}$"),
          dep.var.labels = c("Racial Resentment$_t$", "Partisanship$_t$",
                             "Racial Resentment$_t$", "Partisanship$_t$"), 
          column.labels = c("1992-1994 ANES Low Awareness", "1992-1994 ANES High Awareness", 
                            "2012-2016 VOTER Survey Low Awareness", "2012-2016 VOTER Survey High Awareness"),
          column.separate = c(2,2),
          no.space = T, 
          notes = c("OLS regression results with standard errors in parentheses. Variables scaled 0-1. Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T, 
          digits = 3, df = F, omit.stat = c("f", "adj.rsq"),
          align = T, star.char = c("*"), star.cutoffs = c(0.05), header = F
)